package com.xgq.drink.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xgq.drink.entity.Brand;
import com.xgq.drink.entity.R;
import com.xgq.drink.entity.UserRole;
import com.xgq.drink.service.IBrandService;
import com.xgq.drink.service.IUserRoleService;
import com.xgq.drink.utils.RUtils;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/userRole")
public class UserRoleController {

    @Autowired
    private IUserRoleService userRoleService;
    @Autowired
    private IBrandService brandService;

    @PostMapping("/assign")
    @Operation(summary = "为用户分配角色")
    public R assignRole(@RequestBody UserRole role) {
        if ((role.getRoleId() == 3 || role.getRoleId() == 4)
                && role.getBrandId() == null) {
            return RUtils.fail("店铺角色必须指定brandId");
        }

        UserRole userRole = new UserRole();
        userRole.setUserId(role.getUserId());
        userRole.setRoleId(role.getRoleId());
        userRole.setBrandId(role.getBrandId());

        return userRoleService.save(userRole) ?
                RUtils.success("角色分配成功") : RUtils.fail("角色分配失败");
    }

    @PostMapping("/revoke")
    @Operation(summary = "撤销用户角色")
    public R revokeRole(@RequestParam Long id) {
        return userRoleService.removeById(id) ?
                RUtils.success("角色撤销成功") : RUtils.fail("角色撤销失败");
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户角色")
    public R updateRole(@RequestBody UserRole userRole) {
        if ((userRole.getRoleId() == 3 || userRole.getRoleId() == 4)
                && userRole.getBrandId() == null) {
            return RUtils.fail("店铺角色必须指定brandId");
        }

        return userRoleService.updateById(userRole) ?
                RUtils.success("角色更新成功") : RUtils.fail("角色更新失败");
    }

    @GetMapping("/userRoles")
    @Operation(summary = "获取用户所有角色（含店铺信息）")
    public R getUserRoles(@RequestParam Long userId) {
        List<Map<String, Object>> result = userRoleService.list(new QueryWrapper<UserRole>()
                        .eq("user_id", userId))
                .stream()
                .map(role -> {
                    Map<String, Object> map = new HashMap<>();
                    // 添加基础角色信息
                    map.put("roleId", role.getRoleId());
                    map.put("brandId", role.getBrandId());
                    map.put("createTime", role.getCreateTime());

                    // 当为店铺角色时查询品牌信息
                    if ((role.getRoleId() == 3 || role.getRoleId() == 4) && role.getBrandId() != null) {
                        Brand brand = brandService.getById(role.getBrandId());
                        map.put("brandInfo", brand != null ? brand : Collections.emptyMap());
                    }

                    return map;
                }).collect(Collectors.toList());

        return RUtils.success("查询成功", result);
    }
}